package com.hldsmart.smartda.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.hldsmart.smartda.R;
import com.hldsmart.smartda.adapter.FootPrintAdapter;
import com.hldsmart.smartda.adapter.home.MyquanAdapter;
import com.hldsmart.smartda.base.BaseActivity;
import com.hldsmart.smartda.model.entity.MyQuanEntity;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyQuanActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.iv_myquan_back)
    ImageView iv_myquan_back;
    @Bind(R.id.rv_myquan_list)
    RecyclerView rv_myquan_list;

    private ArrayList<MyQuanEntity> myQuanlist;
    private MyquanAdapter myquanAdapter;
    private LinearLayoutManager LayoutManager;
    private ImageView cursorIv;
    private TextView tab01, tab02, tab03;
    private TextView[] titles;

    /**
     * 偏移量（手机屏幕宽度 / 选项卡总数 - 选项卡长度） / 2
     */
    private int offset = 0;

    /**
     * 下划线图片宽度
     */
    private int lineWidth;

    /**
     * 当前选项卡的位置
     */
    private int current_index = 0;

    /**
     * 选项卡总数
     */
    private static final int TAB_COUNT = 3;



    public static void startMyQuanActivity(Context context) {
        Intent intent = new Intent(context, MyQuanActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_quan);
        ButterKnife.bind(this);

        LayoutManager=new LinearLayoutManager(this);//设置布局管理器  
        LayoutManager.setOrientation(OrientationHelper.VERTICAL);
        rv_myquan_list.setLayoutManager(LayoutManager);//设置为垂直布局，这也是默认的 
        myquanAdapter=new MyquanAdapter();
        rv_myquan_list.setAdapter(myquanAdapter);
        initData();
        setListener();
    }

    private void setListener() {
        cursorIv = (ImageView) findViewById(R.id.iv_tab_bottom_img);
        tab01 = (TextView) findViewById(R.id.tv01);
        tab02 = (TextView) findViewById(R.id.tv02);
        tab03 = (TextView) findViewById(R.id.tv03);

        tab01.setOnClickListener(this);
        tab02.setOnClickListener(this);
        tab03.setOnClickListener(this);
        iv_myquan_back.setOnClickListener(this);
        myquanAdapter.setListener(new MyquanAdapter.OnItemClickedListener() {
            @Override
            public void onPhotoClicked(MyQuanEntity myquanEntity) {
                //页面跳转优惠券详情
                QuanDetailActivity.startQuanDetailActivity(MyQuanActivity.this);

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_myquan_back:
                finish();
                break;

            case R.id.tv01:
                // 避免重复加载
                setMoveImageView(0);
                tab01.setTextColor(Color.rgb(30,189,255));
                tab02.setTextColor(Color.rgb(9,9,9));
                tab03.setTextColor(Color.rgb(9,9,9));
                initData();
                break;
            case R.id.tv02:
                setMoveImageView(1);
                tab02.setTextColor(Color.rgb(30,189,255));
                tab01.setTextColor(Color.rgb(9,9,9));
                tab03.setTextColor(Color.rgb(9,9,9));
                userData();

                break;
            case R.id.tv03:
                setMoveImageView(2);
                tab03.setTextColor(Color.rgb(30,189,255));
                tab01.setTextColor(Color.rgb(9,9,9));
                tab02.setTextColor(Color.rgb(9,9,9));
                outData();
                break;
        }
    }

    /**
     * 初始化数据
     */
    private void initData() {
        myQuanlist=new ArrayList<>();
        myquanAdapter.clear();
        myQuanlist.add(new MyQuanEntity("00324845412454","美特斯邦威","2017-2-24 24:00"));
        myQuanlist.add(new MyQuanEntity("00457890472463","肯德基","2017-2-24 24:00"));
        myQuanlist.add(new MyQuanEntity("00324845412484","森马服装","2017-2-26 24:00"));
        myQuanlist.add(new MyQuanEntity("00324845419047","特步女鞋","2017-3-24 24:00"));
        myQuanlist.add(new MyQuanEntity("00324845448454","美特斯邦威","2017-2-28 24:00"));
        myquanAdapter.addData(myQuanlist);
    }


    /**
     * 已使用数据
     */
    private void userData() {
        myQuanlist=new ArrayList<>();
        myquanAdapter.clear();
        myQuanlist.add(new MyQuanEntity("00578909844545","森马服装","2017-1-24 24:00"));
        myQuanlist.add(new MyQuanEntity("00457890472463","肯德基","2017-1-24 24:00"));
        myQuanlist.add(new MyQuanEntity("43345467879431","特步女鞋","2017-2-24 24:00"));
        myquanAdapter.addData(myQuanlist);
    }

    /**
     * 已过期数据
     */
    private void outData() {
        myQuanlist=new ArrayList<>();
        myquanAdapter.clear();
        myQuanlist.add(new MyQuanEntity("00578909844545","欢乐谷","2017-1-02 13:00"));
        myQuanlist.add(new MyQuanEntity("00457890472463","德克士","2017-1-03 9:00"));
        myQuanlist.add(new MyQuanEntity("66345678765876","森马","2017-1-16 14:30"));
        myQuanlist.add(new MyQuanEntity("34876543234598","特步女鞋","2017-2-14 12:00"));
        myquanAdapter.addData(myQuanlist);
    }

    /**
     * 初始化底部下划线
     */
    private void setMoveImageView(int position) {
        titles = new TextView[]{tab01, tab02, tab03};
        // 获取图片宽度
        lineWidth = BitmapFactory.decodeResource(getResources(), R.drawable.ic_bule_line).getWidth();
        // Android提供的DisplayMetrics可以很方便的获取屏幕分辨率
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels; // 获取分辨率宽度
        offset = (screenW / TAB_COUNT - lineWidth) / 2;  // 计算偏移值
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        // 设置下划线初始位置
        cursorIv.setImageMatrix(matrix);

        int one = offset * 2 + lineWidth;
        // 下划线开始移动前的位置
        float fromX = one * current_index;
        // 下划线移动完毕后的位置
        float toX = one * position;
        Animation animation = new TranslateAnimation(fromX, toX, 0, 0);
        // 设置为true，防止下划线返回，也就是动画终止时让View停留在最后一帧，不然会下划线又会回到没有执行之前的状态
        // 如果不设置该属性，则下划线会回到动画开始的位置
        animation.setFillAfter(true);
        animation.setDuration(300);
        // 给图片添加动画
        cursorIv.startAnimation(animation);
//        titles[position].setTextColor(Color.rgb(30,189,255));
//        titles[current_index].setTextColor(Color.rgb(9,9,9));
        current_index=position;
    }

}
